package com.nit.lab.mapper.basic;

import com.nit.lab.dto.basic.UnitDTO;
import com.nit.lab.entity.basic.UnitEntity;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @Description
 * @Author HWY
 * @Date 2020/12/14 10:50
 **/
public interface UnitMapper {

    /**
     * @Description: 获取单位表格数据
     * @Author HWY
     * @Date 2020/12/14 10:50
     */
    @Select({"<script>SELECT " +
            "unit_id AS `key`, unit_id, " +
            "unit_name, unit_info, " +
            "unit_father, unit_type, gmt_create " +
            "FROM basic_unit " +
            "WHERE 1 = 1 " +
            "<if test='unitName != null'> AND unit_name LIKE CONCAT('%', #{unitName}, '%') </if>" +
            "<if test='unitFather != null'> AND unit_father = #{unitFather} </if>" +
            "<if test='unitType != null'> AND unit_type = #{unitType} </if>" +
            "ORDER BY " +
            "<if test='sortField != null'>" +
            "${sortField} ${sortOrder}, " +
            "</if>" +
            "unit_id " +
            "LIMIT #{start}, #{length}" +
            "</script>"})
    List<UnitDTO> list4Table(
            @Param("start") Integer start,
            @Param("length") Integer length,
            @Param("unitName") String unitName,
            @Param("unitFather") String unitFather,
            @Param("unitType") String unitType,
            @Param("sortField") String sortField,
            @Param("sortOrder") String sortOrder) throws Exception;

    /**
     * @Description: 获取单位表格数据总条数
     * @Author HWY
     * @Date 2020/12/14 10:50
     */
    @Select({"<script>SELECT COUNT(*) " +
            "FROM basic_unit " +
            "WHERE 1 = 1 " +
            "<if test='unitName != null'> AND unit_name LIKE CONCAT('%', #{unitName}, '%') </if>" +
            "<if test='unitFather != null'> AND unit_father = #{unitFather} </if>" +
            "<if test='unitType != null'> AND unit_type = #{unitType} </if>" +
            "</script>"})
    Long count4Table(
            @Param("unitName") String unitName,
            @Param("unitFather") String unitFather,
            @Param("unitType") String unitType) throws Exception;

    /**
     * @Description: 新增单位信息
     * @Author HWY
     * @Date 2020/12/14 10:50
     */
    @Insert("INSERT INTO basic_unit (" +
            "unit_name, " +
            "unit_info, unit_father, " +
            "unit_type, gmt_create) " +
            "VALUES (#{unitEntity.unitName}, " +
            "#{unitEntity.unitInfo}, #{unitEntity.unitFather}, " +
            "#{unitEntity.unitType},  #{unitEntity.gmtCreate})")
    @Options(useGeneratedKeys = true, keyProperty = "unitId", keyColumn = "unit_id")
    Integer add(@Param("unitEntity") UnitEntity unitEntity) throws Exception;

    /**
     * @Description: 更新单位信息
     * @Author HWY
     * @Date 2020/12/14 10:50
     */
    @Update("UPDATE basic_unit " +
            "SET unit_name = #{unitEntity.unitName}, " +
            "unit_info = #{unitEntity.unitInfo}, " +
            "unit_father = #{unitEntity.unitFather}, " +
            "unit_type = #{unitEntity.unitType} " +
            "WHERE unit_id = #{unitEntity.unitId}")
    Integer update(@Param("unitEntity") UnitEntity unitEntity) throws Exception;

    /**
     * @Description: 删除单位信息
     * @Author HWY
     * @Date 2020/12/14 10:50
     */
    @Delete("DELETE FROM basic_unit WHERE unit_id = #{unitId}")
    Integer remove(@Param("unitId") Long unitId) throws Exception;

    /**
     * @Description: 获取单位信息用于选择
     * @Author: HWY
     * @Date: 2020/12/14 22:45
     */
    @Select("SELECT unit_id, unit_name, unit_info, " +
            "unit_father, unit_type, gmt_create " +
            "FROM basic_unit " +
            "WHERE 1 = 1")
    List<UnitDTO> listUnit() throws Exception;

    /**
     * @Description: 获取单位信息树
     * @Author HWY
     * @Date 2020/12/20
     */
    @Select("SELECT unit_id, unit_name, unit_father, unit_type " +
            "FROM basic_unit " +
            "WHERE 1 = 1")
    @Results({
            @Result(column = "unit_id", property = "unitId"),
            @Result(column = "unit_name", property = "unitName"),
            @Result(column = "unit_father", property = "actions"),
            @Result(column = "unit_type", property = "actionList")
    })
    List<UnitDTO> listUnitTree() throws Exception;

    /**
     * @Description: 根据部别(备注)获取单位信息用于数据导入
     * @Author: SN
     * @Date: 2020/12/14 22:45
     */
    @Select("SELECT unit_id, unit_name, unit_info, " +
            "unit_father, unit_type, gmt_create " +
            "FROM basic_unit " +
            "WHERE unit_info LIKE #{unitInfo}")
    UnitDTO getUnitByInfo(@Param("unitInfo") String unitInfo) throws Exception;

    /**
     * @Description: 根据ID获取单位信息用于Word生成
     * @Author: SN
     * @Date: 2020/12/14 22:45
     */
    @Select("SELECT unit_id, unit_name, unit_info, " +
            "unit_father, unit_type, gmt_create " +
            "FROM basic_unit " +
            "WHERE unit_id LIKE #{unitId}")
    UnitDTO getUnitById(Long unitId) throws Exception;
    /**
     * @Description: 根据单位名称获取营列表
     * @Author: ZXQ
     * @Date: 2021/3/4
     */
    @Select("SELECT unit_id, unit_name, unit_info, " +
            "unit_father, unit_type, gmt_create " +
            "FROM basic_unit " +
            "WHERE unit_name LIKE CONCAT('%', #{unitName}, '') ")
    List<UnitDTO> listUnitByUnitName(@Param("unitName") String unitName) throws Exception;

    /**
     * @Description: 根据id获取name
     * @Author: HGJ
     * @Date: 2021/4/15
     */
    @Select("select unit_info from basic_unit where unit_id = #{unitId}")
    String getUnitNameById(@Param("unitId")Long unitId) throws Exception;

    /**
     * @Description: 根据unitId获取下属
     */
    @Select("SELECT getSonUnits(#{unitId})")
    String getSonUnits(@Param("unitId")Long unitId) throws Exception;

}
